package com.xauto.pioneer.appuser.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;

import java.util.ArrayList;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
	ArrayList<Fragment> fragments;
	FragmentManager fm;

	public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
		super(fm);
		this.fm = fm;
		this.fragments = fragments;
	}

	public void setFragments(ArrayList<Fragment> fragments) {
		if (this.fragments != null) {
			FragmentTransaction ft = fm.beginTransaction();
			for (Fragment f : this.fragments) {
				ft.remove(f);
			}
			ft.commit();
			ft = null;
			fm.executePendingTransactions();
		}
		this.fragments = fragments;
		notifyDataSetChanged();
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	@Override
	public Fragment getItem(int arg0) {
		return fragments.get(arg0);
	}

	public void removeALlFragments() {
		FragmentTransaction transaction = fm.beginTransaction();
		for (int i = 0; i < fragments.size(); i++) {
			Fragment fg = fragments.get(i);
			transaction.remove(fg);
		}
		transaction.commit();
		fragments.clear();
	}

	@Override
	public int getCount() {
		return fragments.size();
	}
}